import telnetlib
# ip = '203.0.113.1';username = 'qytang';password = 'qytang'

'''
# cisco
tn = telnetlib.Telnet(ip)
tn.read_until(b'Username:')
tn.write(username.encode('ascii')+b"\n")
tn.read_until(b'Password:')
tn.write(password.encode('ascii')+b"\n")
tn.read_until(b'SW1#').decode('ascii')
tn.write('terminal length 0'.encode('ascii')+b"\n")
tn.read_until(b'SW1#').decode('ascii')
tn.write('show run'.encode('ascii')+b"\n")
aa = tn.read_until(b'SW1#').decode('ascii')
print(aa)
tn.close()
'''

'''
# huawei
tn = telnetlib.Telnet(ip)
tn.read_until(b'Password:')
tn.write(password.encode('ascii')+b"\n")
tn.read_until(b'<SW3>').decode('ascii')
tn.write('screen-length 0 temporary'.encode('ascii')+b"\n")
tn.read_until(b'<SW3>').decode('ascii')
tn.write('dis cur'.encode('ascii')+b"\n")
aa = tn.read_until(b'<SW3>').decode('ascii')
print(aa)
tn.close()
'''

for device in range(len(devicelist)):
    plat = devicelist[device]['platform']
    namet = devicelist[device]['name']
    ipt = devicelist[device]['ip']
    usernamet = devicelist[device]['username']
    passwordt = devicelist[device]['password']
    if plat == 'IOS':
        tn = telnetlib.Telnet(ipt)
        tn.read_until(b'Username:')
        tn.write(usernamet.encode('ascii')+b"\n")
        tn.read_until(b'Password:')
        tn.write(passwordt.encode('ascii')+b"\n")
        tn.read_until(b'#').decode('ascii')
        tn.write('terminal length 0'.encode('ascii')+b"\n")
        tn.read_until(b'#').decode('ascii')
        tn.write('show run'.encode('ascii')+b"\n")
        aa = namet + '\n' + tn.read_until(b'#').decode('ascii')
        tn.close()
        # config = open('/home/qyt/Documents/' +  namet + '.txt',mode='w+')
        # config.write(aa)
        # config.close()

    elif plat == 'VRP':
        tn = telnetlib.Telnet(ipt)
        tn.read_until(b'Password:')
        tn.write(passwordt.encode('ascii') + b"\n")
        tn.read_until(b'>').decode('ascii')
        tn.write('screen-length 0 temporary'.encode('ascii') + b"\n")
        tn.read_until(b'>').decode('ascii')
        tn.write('dis cur'.encode('ascii') + b"\n")
        aa = namet + '\n' +  tn.read_until(b'>').decode('ascii')
        tn.close()
        # config = open('/home/qyt/Documents/' +  namet + '.txt',mode='w+')
        # config.write(aa)
        # config.close()



else:
    print('='*80)
    print('Mission accomplished.')
    print('='*80)

